package com.inspur.dao.impl;

import com.inspur.bean.Category;
import com.inspur.dao.CategoryDAO;
import com.inspur.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：齐文勇
 * @Package：com.inspur.dao.impl
 * @Project：ProductMS
 * @name：CategoryDAOImpl
 * @Date：2024/11/27 14:18
 * @Filename：CategoryDAOImpl
 */
public class CategoryDAOImpl implements CategoryDAO {
    private Connection conn;
    private PreparedStatement pre;
    private ResultSet rs;
    @Override
    public int save(Category category) {
        try{
            conn= JDBCUtil.getConnection();
            String sql = "insert into category (name)values (?)";
            pre = conn.prepareStatement(sql);
            pre.setString(1,category.getName());
            int i = pre.executeUpdate();
            return i;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(conn,pre);
        }
        return 0;
    }

    @Override
    public int deleteById(String  id) {
        try{
            conn= JDBCUtil.getConnection();
            String sql = "delete from category where id =?";
            pre = conn.prepareStatement(sql);
            pre.setString(1,id);
            int i = pre.executeUpdate();
            return i;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(conn,pre);
        }
        return 0;
    }

    @Override
    public Category findById(String id) {
        try{
            conn= JDBCUtil.getConnection();
            String sql = "select id,name  from category where id = "+id;
            pre = conn.prepareStatement(sql);
            rs = pre.executeQuery();
            if (rs.next()){
                Category category = new Category();
                category.setId(rs.getInt("id"));
                category.setName(rs.getString("name"));
                return  category;
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(conn,pre);
        }
        return  null;
    }

    @Override
    public int updateById(Category category) {
        try{
            conn= JDBCUtil.getConnection();
            String sql = "update  category set name =? where id =?";
            pre = conn.prepareStatement(sql);
            pre.setString(1,category.getName());
            pre.setInt(2,category.getId());
            int i = pre.executeUpdate();
            return i;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(conn,pre);
        }
        return 0;
    }

    @Override
    public List<Category> findAll() {
        List<Category> categoryList = new ArrayList<>();
        try{
            conn= JDBCUtil.getConnection();
            String sql = "select id,name  from category";
            pre = conn.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next()){
                Category category = new Category();
                category.setId(rs.getInt("id"));
                category.setName(rs.getString("name"));
                categoryList.add(category);
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(conn,pre);
        }
        return categoryList;
    }
}
